/*
 * Copyright 2011 myTDev.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.mytdev.resources.injection;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.logging.Logger;

/**
 *
 * @author Yann D'Isanto
 */
public class ResourceInjector {

    private static final Logger LOG = Logger.getLogger(ResourceInjector.class.getName());
    private com.mytdev.resources.R resources;
    private Object object;

    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.FIELD, ElementType.TYPE})
    public static @interface R {

        String key() default "";

        String property() default "";
    }

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.FIELD)
    public static @interface Rs {

        R[] value();
    }

    public ResourceInjector(Object object) {
        if (object == null) {
            throw new NullPointerException("object is null");
        }
        this.object = object;
    }

    public Object getObject() {
        return object;
    }

    public void setObject(Object object) {
        if (object == null) {
            throw new NullPointerException("object is null");
        }
        this.object = object;
    }

    public void inject() throws ResourceInjectionException {
        Class<?> objectClass = object.getClass();
        R annotation = objectClass.getAnnotation(R.class);
        resources = annotation == null
                ? com.mytdev.resources.R.getBundle(object.getClass())
                : com.mytdev.resources.R.getBundle(annotation.key());
        for (Field field : objectClass.getDeclaredFields()) {
            injectField(field);
        }
    }

    private void injectField(Field field) throws ResourceInjectionException {
        R[] resourceAnnotations = {};
        R singleResourceAnnotation = field.getAnnotation(R.class);
        if (singleResourceAnnotation != null) {
            resourceAnnotations = new R[]{singleResourceAnnotation};
        } else {
            Rs rs = field.getAnnotation(Rs.class);
            if (rs != null) {
                resourceAnnotations = rs.value();
            }
        }
        for (R resourceAnnotation : resourceAnnotations) {
            String key = resourceAnnotation.key();
            String property = resourceAnnotation.property();
            try {
                if (property.isEmpty()) {
                    if (key.isEmpty()) {
                        key = field.getName();
                    }
                    ResourcesInjectionUtil.setFieldValue(field, object, ResourcesInjectionUtil.getResourceValue(key, field.getType(), resources));
                } else {
                    if (key.isEmpty()) {
                        key = field.getName() + ResourcesInjectionUtil.PROPERTY_SEPARATOR + property;
                    }
                    ResourcesInjectionUtil.setProperty(ResourcesInjectionUtil.getFieldValue(field, object), property, key, resources);
                }
            } catch (IllegalAccessException ex) {
                throw new ResourceInjectionException(ex);
            } catch (InvocationTargetException ex) {
                throw new ResourceInjectionException(ex);
            }
        }
    }
    
}
